package sseGameApps.controller.mdkbr.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slim3.controller.Navigation;

import sseGameApps.controller.JsonController;
import sseGameApps.model.mdkbr.MdKanji;
import sseGameApps.model.mdkbr.MdKbrUser;
import sseGameApps.model.mdkbr.MdUseKanji;
import sseGameApps.service.mdkbr.MdKanjiService;
import sseGameApps.service.mdkbr.MdKbrUserService;

public class MdSchoolTrnmntController extends JsonController {

    private MdKanjiService serviceMdKanji = new MdKanjiService();
    private MdKbrUserService serviceMdKbrUser = new MdKbrUserService();

    @Override
    public Navigation run() throws Exception {

        Map<String, Integer> typeSpecialPointP = new HashMap<String, Integer>();

        MdUseKanji mdUseKanjiP =
            serviceMdKanji.getMdUseKanji(asString("userId"));
        MdKbrUser mdKbrUserP =
            serviceMdKbrUser.getMdKbrUser(asString("userId"));

        Map<String, String> data = new HashMap<String, String>();

        data.put("idCanvas", asString("idCanvas"));
        data.put("userId", asString("userId"));

        data.put("playerName", mdKbrUserP.getPlayerName());
        data.put("playerCG", mdKbrUserP.getPlayerCGType());
        data.put("playerSchool", mdKbrUserP.getPlayerSchool());
        data.put("playerSex", mdKbrUserP.getPlayerSex());
        data.put("playerType", mdKbrUserP.getPlayerType());
        data
            .put(
                "userLevel",
                serviceMdKbrUser
                    .checkUserLevel(mdKbrUserP.getUserExp())
                    .toString());
        data.put("userExp", mdKbrUserP.getUserExp().toString());
        data.put(
            "userNextLvExp",
            serviceMdKbrUser
                .checkUserNextLevelExp(mdKbrUserP.getUserExp())
                .toString());

        typeSpecialPointP = serviceMdKanji.getTypeSpecialPoint(mdUseKanjiP);
        data.put("playerBarPoint", mdUseKanjiP.getUserBp().toString());
        data.put("playerBarPointBefore", mdUseKanjiP.getUserBp().toString());
        data.put("playerAttackPoint", mdUseKanjiP.getUserAp().toString());
        data.put("playerKanji01", mdUseKanjiP.getUsingKanji01());
        data.put("playerKanji02", mdUseKanjiP.getUsingKanji02());
        data.put("playerKanji03", mdUseKanjiP.getUsingKanji03());
        data.put("playerTypeSp1", typeSpecialPointP
            .get("typeSpecialPoint1")
            .toString());
        data.put("playerTypeSp2", typeSpecialPointP
            .get("typeSpecialPoint2")
            .toString());
        data.put("playerTypeSp3", typeSpecialPointP
            .get("typeSpecialPoint3")
            .toString());
        data.put("playerTypeSp4", typeSpecialPointP
            .get("typeSpecialPoint4")
            .toString());
        data.put("playerTypeSp5", typeSpecialPointP
            .get("typeSpecialPoint5")
            .toString());

        Integer enemyBarPoint = 0;
        Integer enemyAttackPoint = 0;

        List<String> kanjiList = new ArrayList<String>();
        Integer typeSp1 = 0;
        Integer typeSp2 = 0;
        Integer typeSp3 = 0;
        Integer typeSp4 = 0;
        Integer typeSp5 = 0;

        // 属性制限
        List<MdKanji> listMdKanji = null;

        if ("1".equals(asString("fieldType"))) {
            // 属性制限
            listMdKanji =
                serviceMdKanji.getListTypeMdKanji(asString("kanjiType"));
            data.put("enemyName", "属性代表");
            data.put("eneId", "属性代表");

        } else if ("2".equals(asString("fieldType"))) {
            // 攻撃力制限
            listMdKanji = serviceMdKanji.getListAttackMdKanji(250, 300);
            data.put("enemyName", "攻撃力代表");
            data.put("eneId", "攻撃力代表");

        } else if ("3".equals(asString("fieldType"))) {

        } else {

        }
        if (listMdKanji.size() < 10) {
            enemyBarPoint =
                listMdKanji.get(0).getBarPoint()
                    + listMdKanji.get(1).getBarPoint()
                    + listMdKanji.get(2).getBarPoint();
            enemyAttackPoint =
                listMdKanji.get(0).getAttackPoint()
                    + listMdKanji.get(1).getAttackPoint()
                    + listMdKanji.get(2).getAttackPoint();
            kanjiList.add(listMdKanji.get(0).getMojiText());
            kanjiList.add(listMdKanji.get(1).getMojiText());
            kanjiList.add(listMdKanji.get(2).getMojiText());

            for (int i = 0; i < 3; i++){
                if ("KI".equals(listMdKanji.get(i).getKanjiType())) {
                    typeSp1++;
                } else if ("HI".equals(listMdKanji.get(i).getKanjiType())) {
                    typeSp2++;
                } else if ("KIN".equals(listMdKanji.get(i).getKanjiType())) {
                    typeSp3++;
                } else if ("DO".equals(listMdKanji.get(i).getKanjiType())) {
                    typeSp4++;
                } else if ("SUI".equals(listMdKanji.get(i).getKanjiType())) {
                    typeSp5++;
                }
            }
        } else {
            switch ((int) (Math.round(Math.random() * 3 + 0.5))) {
            case 1:
                enemyBarPoint =
                    listMdKanji.get(0).getBarPoint()
                        + listMdKanji.get(1).getBarPoint()
                        + listMdKanji.get(2).getBarPoint();
                enemyAttackPoint =
                    listMdKanji.get(0).getAttackPoint()
                        + listMdKanji.get(1).getAttackPoint()
                        + listMdKanji.get(2).getAttackPoint();
                kanjiList.add(listMdKanji.get(0).getMojiText());
                kanjiList.add(listMdKanji.get(1).getMojiText());
                kanjiList.add(listMdKanji.get(2).getMojiText());
                for (int i = 0; i < 3; i++){
                    if ("KI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp1++;
                    } else if ("HI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp2++;
                    } else if ("KIN".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp3++;
                    } else if ("DO".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp4++;
                    } else if ("SUI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp5++;
                    }
                }
                break;
            case 2:
                enemyBarPoint =
                    listMdKanji.get(0).getBarPoint()
                        + listMdKanji.get(2).getBarPoint()
                        + listMdKanji.get(4).getBarPoint();
                enemyAttackPoint =
                    listMdKanji.get(0).getAttackPoint()
                        + listMdKanji.get(2).getAttackPoint()
                        + listMdKanji.get(4).getAttackPoint();
                kanjiList.add(listMdKanji.get(0).getMojiText());
                kanjiList.add(listMdKanji.get(2).getMojiText());
                kanjiList.add(listMdKanji.get(4).getMojiText());
                for (int i = 0; i < 5; i = i + 2){
                    if ("KI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp1++;
                    } else if ("HI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp2++;
                    } else if ("KIN".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp3++;
                    } else if ("DO".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp4++;
                    } else if ("SUI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp5++;
                    }
                }
                break;
            case 3:
                enemyBarPoint =
                    listMdKanji.get(5).getBarPoint()
                        + listMdKanji.get(6).getBarPoint()
                        + listMdKanji.get(7).getBarPoint();
                enemyAttackPoint =
                    listMdKanji.get(5).getAttackPoint()
                        + listMdKanji.get(6).getAttackPoint()
                        + listMdKanji.get(7).getAttackPoint();
                kanjiList.add(listMdKanji.get(5).getMojiText());
                kanjiList.add(listMdKanji.get(6).getMojiText());
                kanjiList.add(listMdKanji.get(7).getMojiText());
                for (int i = 5; i < 8; i++){
                    if ("KI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp1++;
                    } else if ("HI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp2++;
                    } else if ("KIN".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp3++;
                    } else if ("DO".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp4++;
                    } else if ("SUI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp5++;
                    }
                }
                break;
            default:
                enemyBarPoint =
                    listMdKanji.get(7).getBarPoint()
                        + listMdKanji.get(8).getBarPoint()
                        + listMdKanji.get(9).getBarPoint();
                enemyAttackPoint =
                    listMdKanji.get(7).getAttackPoint()
                        + listMdKanji.get(8).getAttackPoint()
                        + listMdKanji.get(9).getAttackPoint();
                kanjiList.add(listMdKanji.get(7).getMojiText());
                kanjiList.add(listMdKanji.get(8).getMojiText());
                kanjiList.add(listMdKanji.get(9).getMojiText());
                for (int i = 7; i < 10; i++){
                    if ("KI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp1++;
                    } else if ("HI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp2++;
                    } else if ("KIN".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp3++;
                    } else if ("DO".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp4++;
                    } else if ("SUI".equals(listMdKanji.get(i).getKanjiType())) {
                        typeSp5++;
                    }
                }
            }
        }

        data.put("enemyType", asString("kanjiType"));

        data.put("enemyBarPoint", enemyBarPoint.toString());
        data.put("enemyBarPointBefore", enemyBarPoint.toString());
        data.put("enemyAttackPoint", enemyAttackPoint.toString());
        data.put("enemyKanji01", kanjiList.get(0));
        data.put("enemyKanji02", kanjiList.get(1));
        data.put("enemyKanji03", kanjiList.get(2));
        data.put("enemyTypeSp1", typeSp1.toString());
        data.put("enemyTypeSp2", typeSp2.toString());
        data.put("enemyTypeSp3", typeSp3.toString());
        data.put("enemyTypeSp4", typeSp4.toString());
        data.put("enemyTypeSp5", typeSp5.toString());

        if ("1".equals(mdKbrUserP.getPlayerSex())) {
            data.put(
                "enemyCG",
                "g" + mdKbrUserP.getPlayerCGType().substring(1, 5) + "L.png");
        } else {
            data.put(
                "enemyCG",
                "m" + mdKbrUserP.getPlayerCGType().substring(1, 5) + "L.png");
        }

        data.put("fieldType", asString("fieldType"));
        data.put("attackStart", "");

        this.toJson(data);

        return null;

    }
}
